/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.grnt.buddy;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.grnt.dbcommon.DataControler.DataControlException;
import org.grnt.dbcommon.DataControler;
import static org.grnt.buddy.BuddiesContext.*;

/**
 * It is a servlet demonstrates how to chang a user's password.
 * @author sayid
 */
public class ChPasswordSlt extends HttpServlet{
   
	String loginName = null;
	Long userId = null;
	String password = null;
	String msg = null;
	private void output(HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
		response.setContentType("text/html");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");

		ServletOutputStream out = response.getOutputStream();

		out.println("<head><title>Just a basic servlet</title></head>");
		out.println("<body>");
		out.println("login is:" + loginName);
		out.println("</br>");
		out.println("user id is:" + userId);
		out.println("</br>");
		out.println("password :" + password);
		out.println("</br>");
		out.println("<H3>");
		out.println(msg);
		out.println("</body>");
		out.close();
	}
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
		ServletOutputStream out = response.getOutputStream();
		loginName = request.getParameter("userName");
		password = request.getParameter("password");
		if ((loginName == null) || ("".equals(loginName.trim()))) {
			msg = "login name is empty.";
			output(response);
			return;
		}

		if ((password == null) || ("".equals(password.trim()))) {
			msg = "password is emtpy.";
			output(response);
			return;
		}
		userId = getUsersIndex().get(loginName);
		if((userId == null) || (userId == 0)) {
			msg = "Can't found the correct user id.";
			output(response);
		}

		try {
			ChangPassword userUpdater = new ChangPassword(userId, password);
			DataControler.update(userUpdater);
			msg = "successful!";
		} catch (DataControlException ex) {
			msg = ex.getMessage();
			throw new ServletException(ex);
		} finally {
			output(response);
		}
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
